package com.xinhuo.service.Impl;

import com.xinhuo.dao.UserDao;
import com.xinhuo.domain.po.User;
import com.xinhuo.domain.vo.PageBean;
import com.xinhuo.service.IUserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements IUserService
{

	@Resource
	private UserDao userDao;

	@Override
	public Serializable save(User user)
	{
		return userDao.save(user);
	}

	@Override
	public List<User> getAllUser()
	{
		return userDao.findAll();
	}


	@Override
	public void updateUser(User user)
	{
		userDao.save(user);
	}

	@Override
	public void deleteUser(Integer id) {
		// TODO Auto-generated method stub
		userDao.deleteById(id);
	}

	@Override
	public PageBean<User> getUserByPage(int page, int rows) {
		// TODO Auto-generated method stub
		Sort sort = new Sort(Sort.Direction.DESC, "correctSum");
		Pageable pageable = PageRequest.of(page-1, rows,sort);
		Page<User> userPage = userDao.findAll(pageable);

		PageBean<User> pageBean = new PageBean<User>(page, rows,
				userPage.getContent(), userPage.getTotalPages());
		return pageBean;
	}

	@Override
	public User findUserByName(String username) {
		// TODO Auto-generated method stub
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("username", username);
//		String hql="from User u where u.username=:username";
		return userDao.findByUsername(username);
	}
}
